#include "keyboard.h"
#include <QPushButton>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QSpacerItem>
#include <QLineEdit>
#include <QSignalMapper>
#include <QDesktopWidget>
#include <QDebug>
#include <QKeyEvent>
#include <QGuiApplication>
#include <QApplication>
#include <QRegExpValidator>
#include <QStackedWidget>

KeyBoard::KeyBoard(QWidget *parent) :
    QDialog(parent)
{
    setWindowFlags(Qt::WindowDoesNotAcceptFocus | Qt::FramelessWindowHint |
                   Qt::WindowStaysOnTopHint | Qt::Tool | Qt::X11BypassWindowManagerHint);
    this->setFocusPolicy(Qt::NoFocus);

    /*
    QStackedWidget *keyWindow;      // 键盘窗口,可以翻页显示
    keyWindow = new QStackedWidget(this);
    //this->setMinimumWidth(1280);
    keyWindow->setFixedSize(1280, 400);
    keyWindow->setMinimumWidth(1280);
    */

    // 获取屏幕尺寸大小
    QDesktopWidget* desktopWidget = QApplication::desktop();
    screenRect = desktopWidget->screenGeometry();

    //4X4布局 BTNSIZE65*4 + 20margin + 40spaceing
    setFixedSize(320, 320);

    setStyle("#4D4D4D", "#292929", "#C2CCD8", "#F0F0F0");   //黑色
    //setStyle("#DEF0FE", "#C0DEF6", "#C0DCF2", "#386487");   //蓝色

    // 初始化窗体控件
    initWidgets();
}

KeyBoard::~KeyBoard()
{

}

void KeyBoard::initWidgets()
{
    QGridLayout *gridLayout = new QGridLayout(this);
    QSignalMapper *signalMapper = new QSignalMapper(this);

    for (int i = 0; i < 10; i++)
    {
        QString text;
        text.sprintf("%d", i);
        numBt[i] = new QPushButton(this);
        numBt[i]->setText(text);        
        numBt[i]->setStyleSheet(QString("min-height:%1px;min-width:%2px").arg(BTN_SIZE).arg(BTN_SIZE));

        connect(numBt[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
        signalMapper->setMapping(numBt[i], Qt::Key_0+i);
    }

    dotBt = new QPushButton(this);
    dotBt->setText(".");
    dotBt->setStyleSheet(QString("min-height:%1px;min-width:%2px").arg(BTN_SIZE).arg(BTN_SIZE));

    minusBt = new QPushButton(this);
    minusBt->setText("±");
    minusBt->setStyleSheet(QString("min-height:%1px;min-width:%2px").arg(BTN_SIZE).arg(BTN_SIZE));

    backspaceBt = new QPushButton(this);
    backspaceBt->setObjectName("Backspace");
    backspaceBt->setIcon(QIcon(":/res/backspace.svg"));
    backspaceBt->setIconSize(QSize(48, 48));
    backspaceBt->setStyleSheet(QString("min-width:%1px;min-height:%2px;font:%3px;").arg(BTN_SIZE).arg(BTN_SIZE).arg(BTN_SIZE*5/12));

    leftBt = new QPushButton(this);
    leftBt->setObjectName("left");
    leftBt->setIcon(QIcon(":/res/left.svg"));
    leftBt->setIconSize(QSize(48, 48));
    leftBt->setStyleSheet(QString("min-width:%1px;min-height:%2px;font:%3px;").arg(BTN_SIZE).arg(BTN_SIZE).arg(BTN_SIZE*5/12));

    rightBt = new QPushButton(this);
    rightBt->setObjectName("left");
    rightBt->setIcon(QIcon(":/res/right.svg"));
    rightBt->setIconSize(QSize(48, 48));
    rightBt->setStyleSheet(QString("min-width:%1px;min-height:%2px;font:%3px;").arg(BTN_SIZE).arg(BTN_SIZE).arg(BTN_SIZE*5/12));

    enterBt = new QPushButton(this);
    enterBt->setObjectName("Enter");     
    enterBt->setIcon(QIcon(":/res/hidekeyboard.svg"));
    enterBt->setIconSize(QSize(48, 48));
    enterBt->setStyleSheet(QString("min-width:%1px;min-height:%2px;font:%3px;").arg(BTN_SIZE).arg(BTN_SIZE).arg(BTN_SIZE*5/12));

//    inputEdit = new QLineEdit(this);

    connect(dotBt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(minusBt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(backspaceBt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(enterBt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(leftBt, SIGNAL(clicked()), signalMapper, SLOT(map()));
    connect(rightBt, SIGNAL(clicked()), signalMapper, SLOT(map()));

    signalMapper->setMapping(dotBt, Qt::Key_Period);    //dot
    signalMapper->setMapping(minusBt, Qt::Key_Minus);
    signalMapper->setMapping(backspaceBt, Qt::Key_Backspace);
    signalMapper->setMapping(enterBt, Qt::Key_Escape);
    signalMapper->setMapping(leftBt, Qt::Key_Left);
    signalMapper->setMapping(rightBt, Qt::Key_Right);

    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(clickButton(int)));

    //gridLayout->setColumnStretch(0,1);
    //gridLayout->setColumnStretch(5,1);

    //gridLayout->addWidget(inputEdit, 0,1,1,4,Qt::AlignCenter);

    gridLayout->addWidget(numBt[7], 0, 1, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[8], 0, 2, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[9], 0, 3, 1, 1, Qt::AlignCenter);

    gridLayout->addWidget(numBt[4], 1, 1, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[5], 1, 2, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[6], 1, 3, 1, 1, Qt::AlignCenter);

    gridLayout->addWidget(numBt[1], 2, 1, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[2], 2, 2, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(numBt[3], 2, 3, 1, 1, Qt::AlignCenter);

    gridLayout->addWidget(numBt[0], 3, 1, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(dotBt,    3, 2, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(minusBt,  3, 3, 1, 1, Qt::AlignCenter);

    gridLayout->addWidget(backspaceBt, 0, 4, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(leftBt, 1, 4, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(rightBt, 2, 4, 1, 1, Qt::AlignCenter);
    gridLayout->addWidget(enterBt, 3, 4, 1, 1, Qt::AlignCenter);

    // 设置行和列间距 外框间距
    gridLayout->setSpacing(5);
    gridLayout->setContentsMargins(8, 5, 8, 5);
}

void KeyBoard::clickButton(int index)
{
     QKeyEvent *keyPressEvent = NULL;

    //如果按下Esc，则关闭键盘
    if(index == Qt::Key_Escape)
    {
        hideKeyboard();
        return;
    }

    //修改为check button
    enterBt->setIcon(QIcon(":/res/check.svg"));

    // 如果是ASCII字符，
    if(index <= 0xFF)
    {
        QString text(QChar((char)index));
        keyPressEvent = new QKeyEvent(QEvent::KeyPress, index,
                                      Qt::NoModifier, text);
    }
    else
    {
        keyPressEvent = new QKeyEvent(QEvent::KeyPress, index,
                                      Qt::NoModifier);
    }

    if (keyPressEvent != NULL)
    {
        SendkeyEvent(keyPressEvent);
    }
}

void KeyBoard::pressKey(int key)
{
    //如果按下Esc，则关闭键盘
    if(key == Qt::Key_Enter)
    {
        hideKeyboard();
    }

    QKeyEvent *keyPressEvent = NULL;

    if(key <= 0xFF)
    {
        QString text(QChar((char)key));
        keyPressEvent = new QKeyEvent(QEvent::KeyPress, key,
                                      Qt::NoModifier, text);
    }
    else
    {
        keyPressEvent = new QKeyEvent(QEvent::KeyPress, key,
                                      Qt::NoModifier);
    }

    if (keyPressEvent != NULL)
    {
        SendkeyEvent(keyPressEvent);
    }
}

void KeyBoard::showKeyboard(QPoint pt, QRect focusWidget)
{
    QWidget::show();
    this->move(pt.x(), pt.y()+focusWidget.height());
}

void KeyBoard::hideKeyboard()
{
    QWidget::hide();
    emit keyboardHide();
}

bool KeyBoard::isVisible() const
{
    return QWidget::isVisible();
}

void KeyBoard::setStyle(QString topColor, QString bottomColor, QString borderColor, QString textColor)
{
    QStringList qss;

    // 设置键盘背景色,从上到下渐变效果
    qss.append(QString("QWidget{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
               .arg("#292929").arg("#0D0D0D"));
    qss.append(QString("QWidget{border:1px solid %1}").arg(borderColor));

    qss.append(QString("QPushButton{background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 %1,stop:1 %2);}")
               .arg(topColor).arg(bottomColor));

    // 设置字按钮字体
    qss.append(QString("QLabel,QPushButton{font:%1px bold; color:%2;}").arg(BTN_SIZE/2).arg(textColor));

    // 按钮边框
    qss.append(QString("QPushButton{border:1px solid %1;}").arg(borderColor));

    // 去掉按钮焦点虚线框和圆角
    qss.append("QPushButton{outline:0px;border-radius:5px;}");

    // 按钮有按下的效果
    qss.append(QString("QPushButton:pressed{padding-left:3px;padding-top:5px;}"));


    this->setStyleSheet(qss.join(""));
}

